package com.controller;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.Archive;
import com.pojo.Filetp;
import com.service.ArchiveService;
import com.service.FileService;

@Controller
public class FileController {
	FileService service1 = new FileService();
	ArchiveService Service2 = new ArchiveService();
   /*显示上传的图片*/
	@RequestMapping(value = "/queryfile.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Filetp> queryfile(
			@RequestParam(value = "id", defaultValue = "") String id) {
		// System.out.println(id);
		List<Filetp> list = service1.queryfile(id);
		// System.out.println(id);
		// System.out.println(list);
		return list;
	}
/*查询到要删除的那个图片*/
	@RequestMapping(value = "/queryonedeltp.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Filetp> queryonedeltp(
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletResponse response) {
		// System.out.println(id);
		List<Filetp> list = service1.queryonedeltp(id);

		return list;
	}
/*删除数据库中file表的就记录同时删除服务器下的图片*/
	@RequestMapping(value = "/deltp.action", method = RequestMethod.POST)
	@ResponseBody
	public String deltp(
			@RequestParam(value = "id", defaultValue = "") String id,
			HttpServletRequest req) {
		List<Filetp> list = service1.queryonedeltp(id);
		System.out.println(list.get(0).getArchive_id()); // archive的archive_id的值
		System.out.println(list.get(0).getName()); // file表的图片的名字

		String filePath = String.format("%s\\%s\\%s\\%s", req.getSession()
				.getServletContext().getRealPath("/"), "upload", list.get(0)
				.getArchive_id(), list.get(0).getName());
		File existsFile = new File(filePath);
		System.out.println(filePath);
		existsFile.delete();
		if (existsFile.exists()) {
			if (existsFile.delete()) {
				System.out.println("删除原始文件：" + filePath);
			} else {
				System.out.println("删除文件失败：" + filePath);
			}
		}
		service1.deltp(id);
		System.out.println("删除");
		return "OK";

	}
}
